package com.cskaoyan.controller;

import com.cskaoyan.bean.*;
import com.cskaoyan.service.AdminCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Tangyin
 * @description:商品类目
 * @since 2022/05/07 13:27
 */
@RestController
@RequestMapping("admin/category")
public class AdminCategoryController {

    @Autowired
    AdminCategoryService adminCategoryService;


    @GetMapping("list")
    public BaseRespVo<CategoryVO> list() {
        CategoryListBean categoryListBean = adminCategoryService.list();
        return BaseRespVo.listOk(categoryListBean);
    }

    @RequestMapping("l1")
    public BaseRespVo l1list() {
        List<CategoryL1> addressDate = adminCategoryService.select();
        CategoryL1Bean categoryL1Bean =  new CategoryL1Bean();
        categoryL1Bean.setList(addressDate);
        return BaseRespVo.ok(categoryL1Bean);
    }

    @RequestMapping("creat")
    public BaseRespVo creat(@RequestBody Category category) {

        int i = adminCategoryService.creat(category);
        if (i == 1) {
            return BaseRespVo.ok(i);
        }
        return BaseRespVo.invalidData();
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody Category category) {

        Category newCategory = adminCategoryService.update(category);

        return BaseRespVo.listOk(newCategory);
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody CategoryVO categoryVO) {
        int i = adminCategoryService.deleteByPrimaryId(categoryVO.getId());

      /*  List<CategoryChildrenBean> children = categoryVO.getChildren();
        Iterator<CategoryChildrenBean> iterator = children.iterator();
        while(iterator.hasNext()){
            CategoryChildrenBean next = iterator.next();
            Integer id = next.getId();
            adminCategoryService.deleteByPrimaryId(id);
        }*/
        /*for(CategoryChildrenBean childrenBean:categoryVO.getChildren()){
            Integer affectedRows = adminCategoryService.deleteByPrimaryId(childrenBean.getId());

                return BaseRespVo.ok(null);

        }*/
        return BaseRespVo.ok(null);
        //return BaseRespVo.invalidData();
    }
}

